package com.spring.redis;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RedisController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    // spring 通过 RedisTemplate 类来 操作redis 的
    // StringRedisTemplate 是 RedisTemplate的子类，专门用来处理 文本数据的

    @GetMapping("/testString")
    @ResponseBody
    public String testString() {
        // 这个 execute 的方法是 spring 给redis留的一条后路来操作 redis 原生的api
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForValue().set("key", "hello");
        redisTemplate.opsForValue().append("key", "world");

        String key = redisTemplate.opsForValue().get("key");
        System.out.println(key);
        return "is ok!";
    }

    @GetMapping("/testList")
    @ResponseBody
    public String testList() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForList().leftPush("key1", "v1");

        redisTemplate.opsForList().rightPush("key1", "v2");
        redisTemplate.opsForList().rightPush("key1", "v3");

        System.out.println(redisTemplate.opsForList().size("key1"));

        List<String> key1 = redisTemplate.opsForList().range("key1", 0, -1);
        System.out.println(key1);

        return "is ok!";
    }

    @GetMapping("/testSet")
    @ResponseBody
    public String testSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key1", "v1","v2","v3");
        System.out.println(redisTemplate.opsForSet().members("key1"));
        System.out.println(redisTemplate.opsForSet().size("key1"));

        return "is ok!";
    }

    @GetMapping("/testHash")
    @ResponseBody
    public String testHash() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("key1", "f1", "v1");
        redisTemplate.opsForHash().put("key1", "f2", "v2");
        redisTemplate.opsForHash().put("key1", "f3", "v3");

        System.out.println(redisTemplate.opsForHash().get("key1", "f1"));
        System.out.println(redisTemplate.opsForHash().delete("key1","f2"));
        System.out.println(redisTemplate.opsForHash().size("key1"));


        return "is ok!";
    }

    @GetMapping("/testZset")
    @ResponseBody
    public String testZset() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("key1","zhangsan",10);
        redisTemplate.opsForZSet().add("key1","lisi",20);
        redisTemplate.opsForZSet().add("key1","wangwu",30);

        System.out.println(redisTemplate.opsForZSet().range("key1", 0, -1));
        System.out.println(redisTemplate.opsForZSet().rank("key1", "zhangsan"));
        System.out.println(redisTemplate.opsForZSet().rangeWithScores("key1", 0, -1));

        return "is ok!";
    }
}

